﻿define(["jquery","bbcare","jqprint",
    "scprit/department/vm/assessmentScreening/bsid3.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"
],function ($,$bb,$jqprint,$ui,$servers) {
    var event = {};
    event.init = function(obj){
        var userId = obj.userId,type = obj.type,answerId = obj.answerId;
        var hisParams = $servers.hisParams(userId,type,'1','');
        var hisFn = function(data){
            for (var i = 0; i < data.rows.length; i++) {
                data.rows[i].testAge = $servers.getAge($servers.dateDiff(data.rows[i].birthday,data.rows[i].testTime));
            }
            $ui.historyInit({"id":"#bsid-history","data":data.rows});
            if(!answerId){
                answerId = $("#bsid-history>li").eq(0).attr("answerId");
                totalFn(userId,type,answerId);
                $("#bsid-history>li").eq(0).addClass("select").siblings().removeClass("select");
            }else{
                checkedFn(answerId);
                totalFn(userId,type,answerId);
            }
            //历史记录列表点击事件：
            $("#bsid-history>li").click(function(){
                $(this).addClass("select").siblings().removeClass("select");
                answerId = $(this).attr("answerId");
                totalFn(userId,type,answerId);
            });
        };
        $servers.history(hisParams,hisFn);
    };
    var totalFn = function(userId,type,answerId){
        getInfo(userId,answerId,type);
        getGuides(userId,type,answerId);
    }
    //获取评估记录详情：
    var getInfo = function(userId,evaId,type){
        var reportParams = $servers.reportParams(userId,evaId);
        var reportFn = function(data){
            var result = JSON.parse(data.data.result);
            var answerResult = JSON.parse(data.data.answerResult);
            var resultTotal = $.extend({},data.data,answerResult);
            var diff = $servers.dateDiff(data.data.birthday,  data.data.testTime);
            var acturalM = $servers.getMonthAge(diff,'');
            var correctM = $servers.getMonthAge(diff,data.data.pregnancyWeek);
            resultTotal.actualM = acturalM.monthAge;
            resultTotal.correctM = correctM.monthAge;
            resultTotal = $.extend({},resultTotal,result);
            resultTotal = $.extend({},resultTotal,answerResult.score);
            var dif= (resultTotal.pregnancyWeek*1 < 37*7 && diff < 2*365) ? diff-(280- resultTotal.pregnancyWeek):diff;
            resultTotal.stage = stageFn(dif);
            $ui.bsidInfo3Init({'id':"#basic-info","data":resultTotal});
            $ui.bsidInfo3Init({'id':"#print-basicInfo","data":resultTotal});
            $ui.subtestsInit({"id":"#test-score","data":resultTotal});
            $ui.subtestsInit({"id":"#print-testScore","data":resultTotal});
            getGuides(userId,type,evaId,dif);
        };
        $servers.report(reportParams,reportFn);
    };
    /*
     * xx月xx天   转为  总天数
     * */
   var stageFn = function(dif){
       var stageArr = [
           {"stage":"A","month":"16d~1m15d"},{"stage":"B","month":"1m16d~2m15d"},
           {"stage":"C","month":"2m16d~3m15d"},{"stage":"D","month":"3m16d~4m15d"},
           {"stage":"E","month":"4m16d~5m15d"},{"stage":"F","month":"5m16d~6m15d"},
           {"stage":"G","month":"6m16d~8m30d"},{"stage":"H","month":"9m0d~10m30d"},
           {"stage":"I","month":"11m0d~13m15d"},{"stage":"J","month":"13m16d~16m15d"},
           {"stage":"K","month":"16m16d~19m15d"},{"stage":"L","month":"19m16d~22m15d"},
           {"stage":"M","month":"22m16d~25m15d"},{"stage":"N","month":"25m16d~28m15d"},
           {"stage":"O","month":"28m16d~32m15d"},{"stage":"P","month":"33m0d~38m30d"},
           {"stage":"Q","month":"39m0d~42m15d"}
       ];
       for(var i = 0;i<stageArr.length;i++){
           var totalDays = totalDaysFn(stageArr[i].month);
           if( totalDays.days0*1 <= dif && totalDays.days1*1 >= dif ){
               return stageArr[i].stage
           }
       }
   }
   var totalDaysFn = function(stageStr){
        var first = stageStr.substring(0,stageStr.indexOf("~"));
        var last = stageStr.substring(stageStr.indexOf("~")+1);
        return {
            "days0":days(first),
            "days1":days(last)
        };
    };
    var days = function(str) {
        var days;
        if (str.indexOf("m") == "-1") {
            days = str.substring(0, str.indexOf("d")) * 1;
        } else {
            days = str.substring(0, str.indexOf("m")) * 30 + str.substring(str.indexOf("m") + 1, str.indexOf("d")) * 1;
        }
        return days;
    }
    //获取指导意见
    var getGuides = function (userId,exam,evaId,age){
        var params = $servers.getGuidesParamsBsid3(userId,exam,evaId,age);
        var guidesFn = function(data){
            var dt = [];
            for(var item in data.data){
                var itemG = [];
                for(var  i = 0 ;i<data.data[item].length;i++){
                    itemG.push({"con":data.data[item][i]});
                }
                dt.push({
                    "title":item,
                    "p":itemG
                })
            }
            $ui.guideInit({"id":"#guides","data":dt});
            $ui.guideInit({"id":"#print-guides","data":dt});
        };
        $servers.getGuides(params,guidesFn);
    };
    var checkedFn = function(answerId){
        $("#bsid-history").find("li").each(function(i){
            var $this=$(this);
            if($this.attr("answerId")==answerId){
                $(this).addClass("select").siblings().removeClass("select");
            }
        });
    };
    return event;
});